#include "bsp.h"

#define STEP_DELAY_MS 		50

static __IO uint32_t fac_us;
static __IO uint32_t fac_ms;

void BSP_Init(void)
{
	nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
	system_clock_config();
	Delay_init();
	LED_Init();
	DEBUG_USART_Init();
	USB_Init();
	BASE_TIM_Init();
}

void Delay_init(void)
{
	/* configure systick */
  systick_clock_source_config(SYSTICK_CLOCK_SOURCE_AHBCLK_NODIV);
  fac_us = system_core_clock / (1000000U);
  fac_ms = fac_us * (1000U);
}

void delay_us(uint32_t nus)
{
	uint32_t temp = 0;
	SysTick->LOAD = (uint32_t)(nus * fac_us);
	SysTick->VAL = 0x00;
	SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
	do
	{
		temp = SysTick->CTRL;
	}
	while((temp & 0x01) && !(temp & (1<<16)));
	SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
	SysTick->VAL = 0x00;
}

void delay_ms(uint16_t nms)
{
	uint32_t temp = 0;
	while(nms)
	{
		if(nms > STEP_DELAY_MS)
		{
			SysTick->LOAD = (uint32_t)(STEP_DELAY_MS * fac_ms);
			nms -= STEP_DELAY_MS;
		}
		else
		{
			SysTick->LOAD = (uint32_t)(nms * fac_ms);
			nms = 0;
		}
		SysTick->VAL = 0x00;
		SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
		do
		{
			temp = SysTick->CTRL;
		}
		while((temp & 0x01) && !(temp & (1 << 16)));
		SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
		SysTick->VAL = 0x00;
	}
}
